package com.okilo.online.app

import android.annotation.SuppressLint
import android.content.Context
import com.okilo.online.app.base.OkiloBaseApp
import com.okilo.online.app.event.OkiloEventPublisher
import com.okilo.online.app.event.room.OkiloPointManager
import com.okilo.online.app.request.EnhancedHttpLoader
import com.okilo.online.app.request.HttpHelper
import com.okilo.online.app.tools.OkiloDateUtil
import com.okilo.online.app.tools.helper.OkiloAnalysisHp
import com.okilo.online.app.tools.log.OkiloLog
import com.okilo.online.app.tools.shared.OkiloSharedHp
import com.okilo.online.app.tools.shared.OkiloSharedKey

/**
 * App
 * @Author wwdeng
 * @CreateTime: 2024/11/18 13:35
 * @Annotation：
 */
class OkiloApplication: OkiloBaseApp() {
    //事件订阅
    lateinit var publisher: OkiloEventPublisher

    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var app: OkiloApplication
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        app = this
        publisher = OkiloEventPublisher()
    }

    override fun onTerminate() {
        OkiloPointManager.getInstance().release()
        super.onTerminate()
    }

    override fun onCreate() {
        super.onCreate()
        //保存APP启动时间
        val launchTime = OkiloDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")
        OkiloSharedHp.sOkiloSharedData(OkiloSharedKey.SP_OKILO_APP_START_TIME,launchTime)

        OkiloSharedHp.sOkiloSharedData(OkiloSharedKey.SP_OKILO_SECRET_KEY,"SFU88eDUzrnKCrYn")
        OkiloSharedHp.sOkiloSharedData(OkiloSharedKey.SP_OKILO_SECRET_IV,"c7WPq7qR5GhFWagr")
        initLibConfigs()
    }

    /**
     * 初始化Lib
     */
    private fun initLibConfigs(){
        OkiloLog.debug(BuildConfig.isDebug)
//        HttpHelper.init(HttpLoader())
        HttpHelper.init(EnhancedHttpLoader())
        //初始化 analysis
        OkiloAnalysisHp.initLibAtLaunch(this)
        OkiloPointManager.init(this)
    }

    /**
     * 获取当前手机屏幕的宽
     */
    fun getPhoneWidth(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.widthPixels
    }

    /**
     * 获取当前手机屏幕的高
     */
    fun getPhoneHeight(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.heightPixels
    }
}